package Java语言.数据结构.表;

import java.util.*;
//按值排序
class SortHashMapAccordingToValue {//用entry对map进行处理

    private static class ValueComparator implements Comparator<Map.Entry<String, Integer>> {
        public int compare(Map.Entry<String, Integer> m, Map.Entry<String, Integer> n) {
            return n.getValue() - m.getValue();
        }
    }

    public static void main(String[] args) {
        HashMap<String, Integer> map = new HashMap<>();
        map.put("a", 1);
        map.put("c", 3);
        map.put("b", 5);
        map.put("f", 7);
        map.put("e", 6);
        map.put("d", 8);
        List<Map.Entry<String, Integer>> list = new ArrayList<>();

        list.addAll(map.entrySet());
        Collections.sort(list, new ValueComparator());
        for (Iterator<Map.Entry<String, Integer>> it = list.iterator(); it.hasNext(); ) {
            System.out.println(it.next());
        }
    }
}